package marchsoft.modules.four.admin.controller;

import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import marchsoft.base.PageVO;
import marchsoft.modules.four.admin.entity.dto.StudentDTO;
import marchsoft.modules.four.admin.entity.dto.StudentDTOH5;
import marchsoft.modules.four.admin.service.StudentService;
import marchsoft.modules.four.common.entity.Student;
import marchsoft.response.Result;
import org.springframework.core.io.Resource;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.core.io.ClassPathResource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

@RequestMapping("/api")
@RestController
@RequiredArgsConstructor
public class StudentController {

    private final StudentService service;
    private final PasswordEncoder passwordEncoder;

    @ApiOperation("分页条件查询")
    @GetMapping("/eStudent")
    public Result<Object> selectAllRe(PageVO pageVO, StudentDTO studentDTO){
        return Result.success(service.queryAll1(pageVO,studentDTO));
    }

    @ApiOperation("查询安全分类名称")
    @GetMapping("/dept/current")
    public Result<Object> selectAllSafeTypeName( ){
        return Result.success(service.selectAllNameSafeType());
    }

    @ApiOperation("根据id删除学生信息")
    @DeleteMapping("/eStudent")
    public Result<Object> delete(@RequestBody List<Integer> ids ){
        service.deleteStudent(ids);
        return Result.success();
    }

    @ApiOperation("新增学生信息")
    @PostMapping("/eStudent")
    public Result<Object> insert(@RequestBody Student student){
        CharSequence ps = "000000";
        String path = "https://seqbs.oss-cn-beijing.aliyuncs.com/xinyuanxingbao/2022-04-14/164990646894851648.jpeg";
        student.setPassword(passwordEncoder.encode(ps));
        student.setAvatarPath(path);
        service.insertStudent(student);
        return Result.success();
    }

    @ApiOperation("更新学生信息")
    @PutMapping("/eStudent")
    public Result<Object> update(@RequestBody Student student){
        service.updateStudent(student);
        return Result.success();
    }

    @ApiOperation("导出数据")
    @GetMapping("/eStudent/download")
    public void doExport(HttpServletResponse response, StudentDTOH5 studentDTOH5) throws Exception{
        service.download(service.queryAll(studentDTOH5), response);
    }

    @ApiOperation(value = "批量导入学生")
    @PostMapping("/eStudent/import")
    public Result<Void> importData(@RequestParam(name = "file") MultipartFile file, HttpServletResponse res) {
        service.importStudents(file, res);
        return Result.success();
    }

    @ApiOperation(value = "下载模板")
    @GetMapping("/eStudent/downloadTemplate")
    public Result<Void> downloadTemplate(HttpServletResponse response) throws Exception {
        String path = "template/excel/studentInfoTemplate.xlsx";
        String fileName = "studentInfoTemplate.xlsx";
/*		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		Resource[] resources = resolver.getResources(path);
		Resource resource=resources[0];*/
        Resource resource = new ClassPathResource(path);
        //获得文件流，因为在jar文件中，不能直接通过文件资源路径拿到文件，但是可以在jar包中拿到文件流
        InputStream stream = resource.getInputStream();
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        // 响应类型,编码
        response.setContentType("application/octet-stream;charset=UTF-8");
        // 形成输出流
        OutputStream osOut = response.getOutputStream();
        copyStream(stream, osOut, true);
        // 刷新此输出流并强制将所有缓冲的输出字节被写出
        osOut.flush();
        // 关闭流
        osOut.close();
        return Result.success();
    }
    public static void copyStream(InputStream ips, OutputStream ops, boolean closeStream) throws Exception {
        byte[] buf = new byte[1024];
        int len = ips.read(buf);
        while (len != -1) {
            ops.write(buf, 0, len);
            len = ips.read(buf);
        }
    }
}
